package cn.com.oceansoft.business.os.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.os.entity.Show;
import cn.com.oceansoft.business.os.entity.ShowResult;
import cn.com.oceansoft.business.os.service.IShowService;
import cn.com.oceansoft.nos.cache.redis.CacheBaseService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by zhangxl on 2016/12/22.
 */

@Controller
@RequestMapping("show")
public class ShowController extends BaseController {

    @Resource
    private CacheBaseService cacheBaseService;

    @Resource
    private IShowService showService;

    @RequestMapping(value = "index", method = GET)
    public String gotoUserIndex(Query query, Model model) {
        model.addAttribute("query", query);
        model.addAttribute("pager", showService.findShowByPage(query));
        return "os/show/show";
    }

    @RequestMapping(value = "list", method = POST)
    public String loadPageByCondition(Query query, Model model) {
        Pager<Show> pager = showService.findShowByPage(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        return "os/show/show";
    }

    @RequestMapping(value = "newP", method = RequestMethod.GET)
    public String openAddShow(Model model) {
        return "os/show/show-new";
    }

    @RequestMapping(value = "new-p", method = RequestMethod.GET)
    public String openAddShowPhone(Query query, Model model) {
        Pager<Show> pager = showService.findShowByPageP(query);
//        query.setOrderDirection
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
//        return "os/show/phone-show";
        return "os/show/show-list";
    }

    @ResponseBody
    @RequestMapping(value = "new", method = POST)
    public Result addShow(Show show, HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest sr = (MultipartHttpServletRequest) request;
        MultipartFile file = sr.getFile("picFile");
        if (file != null) {
            InputStream in = file.getInputStream();
            byte[] b = new byte[in.available()];
            in.read(b);
            String base = new BASE64Encoder().encode(b);
            show.setPic(base);
        }
        return showService.saveShow(show);
    }

    @RequestMapping(value = "edit/{show-id}", method = GET)
    public String gotoShowEdit(@PathVariable("show-id") int showId, Model model) {
        Show show = showService.findShowById(showId);
        model.addAttribute("show", show);
        return "os/show/show-edit";
    }

    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editShow(Show show, HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest sr = (MultipartHttpServletRequest) request;
        MultipartFile file = sr.getFile("picFile");
        if (file != null) {
            InputStream in = file.getInputStream();
            byte[] b = new byte[in.available()];
            in.read(b);
            String base = new BASE64Encoder().encode(b);
            show.setPic(base);
        }

        return showService.updateShow(show);
    }

    @ResponseBody
    @RequestMapping(value = "editP", method = POST)
    public Result editShowP(Show show, HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest sr = (MultipartHttpServletRequest) request;
        MultipartFile file = sr.getFile("picFile");
        List<Show> shows = new ArrayList<Show>();
        if (file != null) {
            InputStream in = file.getInputStream();
            byte[] b = new byte[in.available()];
            in.read(b);
            String base = new BASE64Encoder().encode(b);
            show.setPic(base);
            cacheBaseService.cache("shows", null);
//            System.out.println(show.getId());
//            System.out.println("-----------------");
//            System.out.println("-----------------"+base);
        }

        return showService.updateShowP(show);
    }

    @ResponseBody
    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    public Result del(@PathVariable("id") int showId) {
        showService.delShow(showId);
        return new Result();
    }

    @RequestMapping(value = "show-r", method = RequestMethod.GET)
    public String openShowResult(Query query, Model model) {
        Pager<Show> pager = showService.findShowByPageP(query);
        model.addAttribute("pager", pager);
        return "os/show/show-r";
    }

    @ResponseBody
    @RequestMapping(value = "show-result", method = RequestMethod.GET)
    public JSONArray openResult(HttpServletRequest resquest) {
        List<ShowResult> res = showService.findShowResult();
        int status = (int)cacheBaseService.getObject("status");
        for(ShowResult re : res){
            re.setStatus(status);
        }
        return JSONArray.parseArray(JSON.toJSONString(res));
    }


}
